<?php
class ControllerAccountUser extends Controller {
	private $error = array();

	public function index() {

		if (!$this->customer->isLogged()) {
			$this->session->data['redirect'] = $this->url->link('account/user', '', 'SSL');

			$this->redirect($this->url->link('account/login', '', 'SSL'));
		}

		$this->language->load('account/edit');
	
		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('account/customer');
		
		if($this->customer->getId()){
			$this->load->model('account/customer');
			
			$cus_infos= array();
			
			$cus_infos = $this->model_account_customer->getChildCustomers($this->customer->getId());

			foreach($cus_infos as $cus_info){
				$cus_info['price'] = $this->currency->format($cus_info['price']);
				$cus_info['price2'] = $this->currency->format($cus_info['price2']);
				$cus_info['date_added'] = date('d/m/Y', strtotime($cus_info['date_added']));
				$this->data['customers'][] = $cus_info;
			}
			
			// List user not active
			$cus_notactive= array();
			
			$cus_notactive = $this->model_account_customer->getChildCustomersNotActive($this->customer->getId());

			foreach($cus_notactive as $cus_info){
				$cus_info['price'] = $this->currency->format($cus_info['price']);
				$cus_info['price2'] = $this->currency->format($cus_info['price2']);
				$cus_info['date_added'] = date('d/m/Y', strtotime($cus_info['date_added']));
				$this->data['customers_notactive'][] = $cus_info;
			}
			
			$this->data['back'] = $this->url->link('account/account', '', 'SSL');

			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/user.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/account/user.tpl';
			} else {
				$this->template = 'default/template/account/user.tpl';
			}
			
			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'	
			);
							
			$this->response->setOutput($this->render());	
		}
	}
	
	public function GetCustomer() {
		$json = array();
		$this->load->model('account/customer');
		$customer_nhan = $this->model_account_customer->getCustomerByMDN($this->request->post['user_nhan']);
		if(!$customer_nhan){
			$json['error'] = 'Mã người nhận không tồn tại trong hệ thống';
		}else{
			$json['success'] = $customer_nhan;
		}
		$this->response->setOutput(json_encode($json));		
  	}  
}
?>